
************
*** CPAP ***
************

use ".\temp\sample_CPAP_reg.dta", clear

egen i = group(geo_id) // unit id
gen t = yq // calendar time
gen Ei = yq(2011,1) if cba_round==1 // year when unit is first treated
replace Ei = yq(2013,3) if cba_round==2
gen K = yq-Ei // relative time
gen D = dd // treatment indicator

drop if K>9 & K!=.

* Event
foreach yvar in lg_price lg_qX lg_paymentX {
cap drop Y
gen Y = `yvar'

	if "`yvar'"=="lg_price" {
		local tt "Log Price"
	}
	if "`yvar'"=="lg_qX" {
		local tt "Log Quantity"
	}
	if "`yvar'"=="lg_paymentX" {
		local tt "Log Spending"
	}

// Estimation with did_imputation of Borusyak et al. (2021)
did_imputation Y i t Ei [aweight=pop_FFS_2010], allhorizons pretrend(18) cluster(i)
estimates store bjs // storing the estimates for later
outreg2 using $resdir\reg.dta, dta replace	
*preserve
*use $resdir\reg_dta.dta, clear
*export excel using $resdir\Table.xls, sheet("Table4_DNDCPAP_B_`yvar'") sheetreplace 	
*restore

// Estimation with did_multiplegt of de Chaisemartin and D'Haultfoeuille (2020)
did_multiplegt Y i t D, robust_dynamic dynamic(9) placebo(18) breps(100) cluster(i) weight(pop_FFS_2010)
*outreg2 using $resdir\reg.dta, dta replace	
matrix dcdh_b = e(estimates) // storing the estimates for later
matrix dcdh_v = e(variances)
putexcel set $resdir\Table_DND_CM_`y'.xls, sheet("B") replace
putexcel A1 = matrix(dcdh_b)

	
// Estimation with eventstudyinteract of Sun and Abraham (2020)
sum Ei
cap drop lastcohort L*event F*event
gen lastcohort = Ei==r(max) // dummy for the latest- or never-treated cohort
forvalues l = 0/9 {
	gen L`l'event = K==`l'
}
forvalues l = 1/18 {
	gen F`l'event = K==-`l'
}
drop F1event // normalize K=-1 (and also K=-15) to zero
eventstudyinteract Y L*event F*event [aweight=pop_FFS_2010], vce(cluster i) absorb(i t) cohort(Ei) control_cohort(lastcohort)
matrix sa_b = e(b_iw) // storing the estimates for later
matrix sa_v = e(V_iw)
*outreg2 using $resdir\reg.dta, dta replace	
*preserve
*use $resdir\reg_dta.dta, clear
*export excel using $resdir\Table.xls, sheet("Table4_DNDCPAP_SA_`yvar'") sheetreplace 	
*restore


// TWFE OLS estimation 
reghdfe Y F*event L*event [aweight=pop_FFS_2010], a(i t) cluster(i)
estimates store ols // saving the estimates for later
*outreg2 using $resdir\reg.dta, dta replace	
*preserve
*use $resdir\reg_dta.dta, clear
*export excel using $resdir\Table.xls, sheet("Table4_DNDCPAP_`yvar'") sheetreplace 	
*restore


// Combine all plots using the stored estimates
event_plot bjs dcdh_b#dcdh_v sa_b#sa_v ols, ///
	stub_lag(tau# Effect_# L#event L#event) stub_lead(pre# Placebo_# F#event F#event) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.325(0.13)0.325) trimlead(18) noautolegend ///
	graph_opt(title("`tt'", size(medlarge)) ///
		xtitle("Relative Qtr to CBP") xlabel(-20(4)8) ///
		legend(order(7 "Baseline" 1 "Borusyak et al." 3 "de Chaisemartin-D'Haultfoeuille" ///
				5 "Sun-Abraham") rows(3) region(style(none))) ///
		xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) ///
		graphregion(color(white)) bgcolor(white) ///
	) ///
	lag_opt1(msymbol(sh) color(cranberry)) lag_ci_opt1(color(cranberry)) ///
	lag_opt2(msymbol(oh) color(forest_green)) lag_ci_opt2(color(forest_green)) ///
	lag_opt3(msymbol(dh) color(navy)) lag_ci_opt3(color(navy)) ///
	lag_opt4(msymbol(o) color(black)) lag_ci_opt4(color(black)) 
	
graph export "$resdir\FigA2_CPAP_`yvar'.png", replace
graph export "$resdir\FigA2_CPAP_`yvar'.pdf", replace

}

